# 
# Description:
# This script constructs tables 1-3
# from the main body of the paper.
#
# Requires: pres_all_terror.dta, terror_pres_wide.dta
#
# Author: Sean Nossek
#
# Date: August 04, 2020

# Load Packages

if (!require("pacman")) install.packages("pacman")

#lfe version manage
packageurl <- "https://cran.r-project.org/src/contrib/Archive/lfe/lfe_2.8.tar.gz"
install.packages(packageurl, repos=NULL, type="source")
library(lfe)

pacman::p_load(readxl,
               tidyverse,
               readstata13,
               stargazer,
               margins,
               lubridate,
               gridExtra)


# Set Working Directory
setwd("C:/Users/Sean/Dropbox/Eran/Terrorism_Left/Paper/Revisions/Revised Paper/Replication/Paper Data")


# Load panel data for table 1

pres_all <- read.dta13("pres_all_terror.dta") %>%
  mutate(days_till_election_last = ifelse(is.na(days_till_election_last), 0, days_till_election_last),
         two_party_share = rep_perc_vote + dem_perc_vote,
         rep_tp_share = (rep_perc_vote / two_party_share) * 100,
         county_fips = str_pad(county_fips, 3, pad = "0"),
         fips = paste0(state_fips, county_fips))

# Code incumbent party

pres_all$incumbent[pres_all$election_year == 1972] <- "R"
pres_all$incumbent[pres_all$election_year == 1976] <- "R"
pres_all$incumbent[pres_all$election_year == 1980] <- "D"
pres_all$incumbent[pres_all$election_year == 1984] <- "R"
pres_all$incumbent[pres_all$election_year == 1988] <- "R"
pres_all$incumbent[pres_all$election_year == 1992] <- "R"
pres_all$incumbent[pres_all$election_year == 1996] <- "D"
pres_all$incumbent[pres_all$election_year == 2000] <- "D"
pres_all$incumbent[pres_all$election_year == 2004] <- "R"
pres_all$incumbent[pres_all$election_year == 2008] <- "R"
pres_all$incumbent[pres_all$election_year == 2012] <- "D"
pres_all$incumbent[pres_all$election_year == 2016] <- "D"

pres_all$incumbent_rep <- as.numeric(pres_all$incumbent == "R")

pres_all$trend <- as.numeric(pres_all$election_year - min(pres_all$election_year))

pres_all$GeoFIPS <- str_pad(pres_all$fips,5,side = "left", "0")

# Add different attack windows

pres_all_counts <- read.dta13("terror_pres_wide.dta")

pres_all_counts$propvalue <- as.numeric(pres_all_counts$propvalue) / 1000000

pres_all_counts$nkill_270 <- pres_all_counts$nkill
pres_all_counts$nkill_180 <- pres_all_counts$nkill  
pres_all_counts$nkill_90 <- pres_all_counts$nkill

pres_all_counts$nkill_270[pres_all_counts$event_to_t2 > 270] <- 0
pres_all_counts$nkill_180[pres_all_counts$event_to_t2 > 180] <- 0
pres_all_counts$nkill_90[pres_all_counts$event_to_t2 > 90] <- 0

pres_all_counts$propvalue_270 <- pres_all_counts$propvalue
pres_all_counts$propvalue_180 <- pres_all_counts$propvalue  
pres_all_counts$propvalue_90 <- pres_all_counts$propvalue

pres_all_counts$propvalue_270[pres_all_counts$event_to_t2 > 270] <- 0
pres_all_counts$propvalue_180[pres_all_counts$event_to_t2 > 180] <- 0
pres_all_counts$propvalue_90[pres_all_counts$event_to_t2 > 90] <- 0

pres_all_counts <- pres_all_counts %>%
  filter(success == 1) %>%
  select(year_t2, FIPSCounty, success, event_to_t2, nkill_270, nkill_180, nkill_90, propvalue_270, propvalue_180, propvalue_90) %>%
  group_by(year_t2, FIPSCounty) %>%
  summarise(success_270 = sum(event_to_t2 <= 270),
            success_180 = sum(event_to_t2 <= 180),
            success_90 = sum(event_to_t2 <= 90), success_30 = sum(event_to_t2 <= 30),
            nkill_270 = sum(nkill_270),
            nkill_180 = sum(nkill_180),
            nkill_90 = sum(nkill_90),
            ropvalue_270 = sum(propvalue_270),
            propvalue_180 = sum(propvalue_180),
            propvalue_90 = sum(propvalue_90)) %>%
  arrange(FIPSCounty, year_t2) %>%
  rename(fips = FIPSCounty, election_year = year_t2)

pres_all <- merge(pres_all, pres_all_counts, by = c("fips","election_year"), all.x = TRUE) %>%
  mutate(success_270 = ifelse(is.na(success_270), 0, success_270), 
         success_180 = ifelse(is.na(success_180), 0, success_180),
         success_90 = ifelse(is.na(success_90), 0, success_90),
         success_30 = ifelse(is.na(success_30), 0, success_30),
         nkill_270 = ifelse(is.na(nkill_270), 0, nkill_270),
         nkill_180 = ifelse(is.na(nkill_270), 0, nkill_180),
         nkill_90 = ifelse(is.na(nkill_270), 0, nkill_90))

pres_all$nkill_270[is.na(pres_all$nkill_270)] <- 0
pres_all$nkill_180[is.na(pres_all$nkill_180)] <- 0
pres_all$nkill_90[is.na(pres_all$nkill_90)] <- 0

pres_all$propvalue_270[is.na(pres_all$propvalue_270)] <- 0
pres_all$propvalue_180[is.na(pres_all$propvalue_180)] <- 0
pres_all$propvalue_90[is.na(pres_all$propvalue_90)] <- 0

# Table 1.


mod1 <- felm(rep_tp_share ~ success_count | fips + election_year | 0 | fips, data = pres_all)
mod2 <- felm(rep_tp_share ~ success_count | fips + election_year | 0 | fips, data = pres_all %>% filter(days_till_election_last <= 270) %>% mutate(success_count = success_270))
mod3 <- felm(rep_tp_share ~ success_count | fips + election_year | 0 | fips, data = pres_all %>% filter(days_till_election_last <= 180) %>% mutate(success_count = success_180))
mod4 <- felm(rep_tp_share ~ success_count | fips + election_year | 0 | fips, data = pres_all %>% filter(days_till_election_last <= 90) %>% mutate(success_count = success_90))

stargazer(mod1, mod2, mod3, mod4, type = "latex", dep.var.labels = "Republican Two-Party Vote Share", dep.var.caption = "", title = "Year and County FEs, County Clustered SEs",
          column.labels = c("All Attacks","9 months","6 months","3 months"))



# load attack level data for table 2

pres_wide <- read.dta13("terror_pres_wide.dta") %>%
  mutate(two_party_share = rep_perc_vote_t2 + dem_perc_vote_t2,
         rep_tp_share = (rep_perc_vote_t2 / two_party_share) * 100,
         success_unknown = as.numeric(success == 1 & motive1 == "Unknown"),
         success_political = as.numeric(success == 1 & motive1 == "Political"),
         success_hatred = as.numeric(success == 1 & motive1 == "Hatred"),
         success_abortion = as.numeric(success == 1 & motive1 == "Anti-Abortion"),
         election_year = year(election_date_t2)) %>% 
  filter(motive1 != "", motive1 != "Religious")

# code incumbency

pres_wide$incumbent[pres_wide$election_year == 1972] <- "R"
pres_wide$incumbent[pres_wide$election_year == 1976] <- "R"
pres_wide$incumbent[pres_wide$election_year == 1980] <- "D"
pres_wide$incumbent[pres_wide$election_year == 1984] <- "R"
pres_wide$incumbent[pres_wide$election_year == 1988] <- "R"
pres_wide$incumbent[pres_wide$election_year == 1992] <- "R"
pres_wide$incumbent[pres_wide$election_year == 1996] <- "D"
pres_wide$incumbent[pres_wide$election_year == 2000] <- "D"
pres_wide$incumbent[pres_wide$election_year == 2004] <- "R"
pres_wide$incumbent[pres_wide$election_year == 2008] <- "R"
pres_wide$incumbent[pres_wide$election_year == 2012] <- "D"
pres_wide$incumbent[pres_wide$election_year == 2016] <- "D"

pres_wide$incumbent_rep <- as.numeric(pres_wide$incumbent == "R")


# Table 2.

mod1<- felm(rep_tp_share ~ success | year + FIPSCounty | 0 | FIPSCounty, data = pres_wide)
mod4<- felm(rep_tp_share ~ success | year + FIPSCounty | 0 | FIPSCounty, data = pres_wide, subset = (event_to_t2 <= 270))
mod5<- felm(rep_tp_share ~ success | year + FIPSCounty | 0 | FIPSCounty, data = pres_wide, subset = (event_to_t2 <= 180))
mod6<- felm(rep_tp_share ~ success | year + FIPSCounty | 0 | FIPSCounty, data = pres_wide, subset = (event_to_t2 <= 90))

stargazer(mod1, mod4, mod5, mod6, type = "latex", dep.var.labels = "Republican Two-Party Vote Share", dep.var.caption = "", title = "County and Year FEs, County Clustered SEs",
          column.labels = c("All Attacks","9 months","6 months","3 months"))


# Table 3.

#1-6
mod1<- felm(rep_tp_share ~ success | year + FIPSCounty | 0 | FIPSCounty, data = pres_wide, subset = (motive2 == "Left-Wing"))
mod2<- felm(rep_tp_share ~ success + success:incumbent_rep | year + FIPSCounty | 0 | FIPSCounty, data = pres_wide, subset = (motive2 == "Left-Wing"))
mod3<- felm(rep_tp_share ~ success | year + FIPSCounty | 0 | FIPSCounty, data = pres_wide, subset = (motive2 == "Anti-Abortion"))
mod4<- felm(rep_tp_share ~ success + success:incumbent_rep | year + FIPSCounty | 0 | FIPSCounty, data = pres_wide, subset = (motive2 == "Anti-Abortion"))
mod5<- felm(rep_tp_share ~ success | year + FIPSCounty | 0 | FIPSCounty, data = pres_wide, subset = (motive2 == "Racial Animosity"))
mod6<- felm(rep_tp_share ~ success + success:incumbent_rep | year + FIPSCounty | 0 | FIPSCounty, data = pres_wide, subset = (motive2 == "Racial Animosity"))
#mod5<- felm(rep_tp_share ~ success | year + FIPSCounty | 0 | FIPSCounty, data = pres_wide, subset = (motive2 == "Racial Animosity" & !(gname %in% c("Zebra Killers","Black Nationalists"))))
#mod6<- felm(rep_tp_share ~ success + success*incumbent_rep | year + FIPSCounty | 0 | FIPSCounty, data = pres_wide, subset = (motive2 == "Racial Animosity" & !(gname %in% c("Zebra Killers","Black Nationalists"))))

#7-12
mod7<- felm(rep_tp_share ~ success | year + FIPSCounty | 0 | FIPSCounty, data = pres_wide, subset = (motive2 == "Puerto Rico"))
mod8<- felm(rep_tp_share ~ success + success:incumbent_rep | year + FIPSCounty | 0 | FIPSCounty, data = pres_wide, subset = (motive2 == "Puerto Rico"))
mod9<- felm(rep_tp_share ~ success | year + FIPSCounty | 0 | FIPSCounty, data = pres_wide, subset = (motive2 == "Cuba"))
mod10<- felm(rep_tp_share ~ success + success:incumbent_rep | year + FIPSCounty | 0 | FIPSCounty, data = pres_wide, subset = (motive2 == "Cuba"))
mod11<- felm(rep_tp_share ~ success | year + FIPSCounty | 0 | FIPSCounty, data = pres_wide, subset = (motive2 == "Environmental"))
mod12<- felm(rep_tp_share ~ success + success:incumbent_rep | year + FIPSCounty | 0 | FIPSCounty, data = pres_wide, subset = (motive2 == "Environmental"))

#13-18
mod13<- felm(rep_tp_share ~ success | year + FIPSCounty | 0 | FIPSCounty, data = pres_wide, subset = (motive2 == "Jewish Right Wing"))
mod14<- felm(rep_tp_share ~ success + success:incumbent_rep | year + FIPSCounty | 0 | FIPSCounty, data = pres_wide, subset = (motive2 == "Jewish Right Wing"))
mod15<- felm(rep_tp_share ~ success | year + FIPSCounty | 0 | FIPSCounty, data = pres_wide, subset = (motive2 == "Black Nationalism"))
mod16<- felm(rep_tp_share ~ success + success:incumbent_rep | year + FIPSCounty | 0 | FIPSCounty, data = pres_wide, subset = (motive2 == "Black Nationalism"))
# mod15<- felm(rep_tp_share ~ success | year + FIPSCounty | 0 | FIPSCounty, data = pres_wide, subset = (motive2 == "Black Nationalism" | gname == "Black Nationalists"))
# mod16<- felm(rep_tp_share ~ success + success*incumbent_rep | year + FIPSCounty | 0 | FIPSCounty, data = pres_wide, subset = (motive2 == "Black Nationalism"| gname == "Black Nationalists"))
mod17<- felm(rep_tp_share ~ success | year + FIPSCounty | 0 | FIPSCounty, data = pres_wide, subset = (motive2 == "Animal Rights"))
mod18<- felm(rep_tp_share ~ success + success:incumbent_rep | year + FIPSCounty | 0 | FIPSCounty, data = pres_wide, subset = (motive2 == "Animal Rights"))



stargazer(mod1, mod2, mod3, mod4, mod5, mod6, type = "latex",  star.char = c("*", "**", "***"),
          star.cutoffs = c(.05, .01, .001),  dep.var.labels = "Republican Two-Party Vote Share",
          column.labels = c("Left-Wing","Anti-Abortion","Racial Animosity"), column.separate = c(2,2,2))

stargazer(mod7, mod8, mod9, mod10, mod11, mod12, type = "latex", star.char = c("*", "**", "***"),
          star.cutoffs = c(.05, .01, .001),  dep.var.labels = "Republican Two-Party Vote Share",
          column.labels = c("Puerto Rico","Cuba","Environmental"), column.separate = c(2,2,2))

stargazer(mod13, mod14, mod15, mod16, mod17, mod18, type = "latex", star.char = c("*", "**", "***"),
          star.cutoffs = c(.05, .01, .001),  dep.var.labels = "Republican Two-Party Vote Share",
          column.labels = c("Jewish Right-Wing","Black Nationalism","Animal Rights"), column.separate = c(2,2,2))
